package matrixCalculator;

import java.util.Vector;


/**
 * This class is in charge of consuming connections and creating the petitions for the IO thread
 * The petitions are stored in the MatrixOperationBuffer 
 * 
 * **/
class ConnectionsHandle extends Thread {
	private Vector<MatrixOperation> MatrixOperationBuffer;
	private Vector<MatrixOperation> MatrixResultsBuffer;
	
	public ConnectionsHandle (Vector<MatrixOperation> MatrixOperationBuffer,Vector<MatrixOperation> MatrixResultsBuffer){
		this.MatrixOperationBuffer=MatrixOperationBuffer;
		this.MatrixResultsBuffer=MatrixResultsBuffer;
	}
	
	public void run(){
		//This is the IO Thread that will consume the matrix Operations launching them in threads that share files.
		PerPetitionIO perPetitionIO = new PerPetitionIO(this.MatrixOperationBuffer, this.MatrixResultsBuffer);
		perPetitionIO.start();
		
		
		//answering client connections stored in the queue
		while(true){
			if(!MatrixCalculatorFirstImplementation.requestQueue.isEmpty()){
				ConnectionThread connection = new ConnectionThread(MatrixCalculatorFirstImplementation.requestQueue.get(0), MatrixOperationBuffer,MatrixResultsBuffer);
				MatrixCalculatorFirstImplementation.requestQueue.remove(0);
				connection.start();
				if(connection.isAlive()){
					try {
						Thread.sleep(50);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				
			}
			
		}
	}
}